package org.jeecg.com.util;

import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.experimental.UtilityClass;
import org.jeecg.modules.wxuser.dto.ThirdSession;

/**
 * sessionToken工具类
 */
@UtilityClass
public class ThirdSessionHolder {

	private final ThreadLocal<ThirdSession> THREAD_LOCAL_THIRD_SESSION = new TransmittableThreadLocal<>();

	/**
	 * TTL 设置thirdSession
	 *
	 * @param thirdSession
	 */
	public void setThirdSession(ThirdSession thirdSession) {
		THREAD_LOCAL_THIRD_SESSION.set(thirdSession);
	}

	/**
	 * 获取TTL中的thirdSession
	 *
	 * @return
	 */
	public ThirdSession getThirdSession() {
		ThirdSession thirdSession = THREAD_LOCAL_THIRD_SESSION.get();
		return thirdSession;
	}

	/**
	 * 获取用户商城ID
	 * @return
	 */
	public String getUserId(){
		if(getThirdSession() != null){
			return getThirdSession().getUserId();
		}
		return null;
	}
	public void clear() {
		THREAD_LOCAL_THIRD_SESSION.remove();
	}
}
